package Servlets;

import Service.RegisterServiceimpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet("/registerservlet")

public class Registerservlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    public Registerservlet() {

        super();
    }


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");

        response.setContentType("text/html;charset=UTF-8");

        String name = request.getParameter("name");


        String i = request.getParameter("id");

        int id = Integer.parseInt(i);
        String admain = request.getParameter("job");
        String password1 = request.getParameter("password1");
        String password2 = request.getParameter("password2");
        String cw = null;
        int ad = 0;
        RegisterServiceimpl rsi = new RegisterServiceimpl();

        try {
            if (admain.equals("我要求职")) {
                ad = 1;
            } else {
                ad = 0;
            }

            cw = rsi.register(id, name, password1, ad);

            if (cw.equals("注册成功")) {

                request.getRequestDispatcher("login.jsp").forward(request, response);

            } else if (cw.equals("账号已存在")) {

                request.setAttribute("cw2", "账号已存在");

                request.getRequestDispatcher("login.jsp").forward(request, response);

            }

        } catch (Exception e) {

// TODO Auto-generated catch block

            e.printStackTrace();

        }

    }


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

        doGet(request, response);

    }


}
